﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.Charge.Impl;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.Charge.ChargeRecord;
using WaterRevenueMS.Core.Service.Charge;
using WaterRevenueMS.Core.Service.Customer;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.Charge;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Domain.ViewModels.Customer.Customer;
using WaterRevenueMS.Core.Service.BasicSet;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using WaterRevenueMS.WebUI.Models;

namespace WaterRevenueMS.WebUI.Controllers
{

    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class ChargeSearchViewController : AdminController
    {
        [Import]
        public IChargeRecordService ChargeRecordService { get; set; }
        [Import]
        private ICustomerService WaterCaliberService { get; set; }


        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var modell = new ChargeRecordModel();
            return View(modell);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = ChargeRecordService.ChargeRecords.Count(t => t.Jiao_FZT == 2);

            var expr = BuildSearchCriteria();

            var filterResult = ChargeRecordService.ChargeRecords.Where(expr).Select(t => new ChargeRecordModel
            {
                Id = t.Id,
                Bei_Z = t.Bei_Z,
                CreateDT = t.CreateDT,
                Yong_HYE = t.Customer != null ? t.Customer.Zhang_HYE : 0,
                Ben_QDS = t.Ben_QDS,
                //     Jiao_FLX = t.Jiao_FLX,
                Shang_QDS = t.Shang_QDS,
                Ben_QYL = t.Ben_QYL,
                He_JYL = t.He_JYL,
                Ben_CJF = t.Ben_CJF,
                UserName = t.Customer != null ? t.Customer.Hu_ZXM : "",
                Yong_HDZ = t.Customer != null ? t.Customer.Yong_HZZ : "",
                ReviseDT = t.ReviseDT,
                Jiao_FZT = t.Jiao_FZT,
                Jiao_FJE = t.Jiao_FJE


            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.UserName,  
                            c.Yong_HDZ,
                            c.Yong_HYE.ToString(),
                            c.Ben_QDS.ToString(),
                            c.Shang_QDS.ToString(),
                            c.Ben_QYL.ToString(),
                            c.He_JYL.ToString(),
                            c.Ben_CJF.ToString(),
                            c.Jiao_FJE.ToString(),
                            c.CreateDT.ToString(),
                            c.ReviseDT.ToString(),
                            c.Jiao_FZTLX,
                            c.Bei_Z,
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }



        public ActionResult Create()
        {
            var model = new ChargeRecordModel();

            return PartialView(model);
        }

        public ActionResult Edit(int Id)
        {
            var model = new ChargeRecordModel();
            var entity = ChargeRecordService.ChargeRecords.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new ChargeRecordModel
                {
                    Bei_Z = entity.Bei_Z,
                    Id = entity.Id,
                    Jiao_FJE = entity.Jiao_FJE

                };

                model.UserName = entity.Customer.Hu_ZXM;

                model.User_ID = (int)entity.User_Id;

            }
            return PartialView(model);
        }
        [HttpPost]
        public ActionResult Edit(ChargeRecordModel model)
        {
            if (ModelState.IsValid)
            {
                ChargeRecordService.GetLiuSModel(model);
                ChargeRecordService.ChangeYE(model); //更改余额

                OperationResult result = ChargeRecordService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }


            }
            else
            {
                return PartialView(model);
            }
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public void Payment(int Id)
        {
            ReportDocument rd = new ReportDocument();
            string strReportName = "Payment.rpt";
            string strRptPath = System.Web.HttpContext.Current.Server.MapPath("~/") + "Rpts//" + strReportName;
            rd.Load(strRptPath);
            var entity = ChargeRecordService.ChargeRecords.FirstOrDefault(t => t.Id == Id);
            if (entity != null)
            {
                var item = new ChargeRecordModel
                {
                    // Chao_BDT = t.CreateDT != null ? Convert.ToDateTime(t.CreateDT.ToString()).ToString("yyyy-MM-dd HH:mm") : "",
                    Chao_BDT = entity.CreateDT.ToString("yyyy-MM-dd HH:mm"),
                    CreateDT = entity.CreateDT,
                    Yong_HYE = entity.Customer != null ? entity.Customer.Zhang_HYE : 0,
                    Ben_QDS = entity.Ben_QDS != null ? entity.Ben_QDS : 0,
                    WaterTpye = entity.KindOfWaterUse.Yong_SXZ != null ? entity.KindOfWaterUse.Yong_SXZ : "",
                    WaterPrice = entity.KindOfWaterUse != null ? entity.KindOfWaterUse.Yong_SDJ.ToString() : "",
                    //     Jiao_FLX = entity.Jiao_FLX,
                    Shang_QDS = entity.Shang_QDS != null ? entity.Shang_QDS : 0,
                    Ben_QYL = entity.Ben_QYL != null ? entity.Ben_QYL : 0,
                    He_JYL = entity.He_JYL != null ? entity.He_JYL : 0,
                    Ben_CJF = entity.Ben_CJF != null ? entity.Ben_CJF : 0,
                    UserName = entity.Customer != null ? entity.Customer.Hu_ZXM : "",
                    Yong_HDZ = entity.Customer != null ? entity.Customer.Yong_HZZ : "",
                    Liu_SH = entity.Liu_SH != null ? entity.Liu_SH : "",
                    User_NO = entity.Customer != null ? entity.Customer.Yong_HBH : "",
                    Meter_NO = entity.Meter_No != null ? entity.Meter_No : "",
                    Jiao_FJE = entity.Jiao_FJE != null ? entity.Jiao_FJE : 0,
                    ReviseDT = entity.ReviseDT,
                    Jiao_FDT = entity.ReviseDT.ToString("yyyy-MM-dd HH:mm"),
                    Shou_FY = entity.Shou_FY != null ? entity.Shou_FY : ""
                };
                Fee fee = new Fee();
                float up_ye;
                up_ye = item.Yong_HYE - item.Jiao_FJE;
                string UpperJe;
                UpperJe = PublicHelper.MoneyToUpper(item.Jiao_FJE.ToString());
                fee.Payment.AddPaymentRow(item.UserName, item.Ben_QDS.ToString(), item.Yong_HDZ, item.Shang_QDS.ToString(), item.WaterTpye, item.Yong_HDZ, item.User_NO, item.Chao_BDT.ToString(), item.Liu_SH, item.Ben_QYL.ToString(), item.WaterPrice, item.Ben_CJF.ToString(), item.Yong_HYE.ToString(), item.Jiao_FDT.ToString(), item.Shou_FY.ToString(), item.Meter_NO, item.Jiao_FJE.ToString(), up_ye.ToString(), UpperJe);
                rd.SetDataSource(fee);
                rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, item.UserName+"crReport");

            }
        }

        private Expression<Func<ChargeRecord, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<ChargeRecord> bulider = new DynamicLambda<ChargeRecord>();
            Expression<Func<ChargeRecord, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["UserName"]))
            {
                var data = Request["UserName"].Trim();
                Expression<Func<ChargeRecord, Boolean>> tmp = t => t.Customer.Hu_ZXM.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }


            Expression<Func<ChargeRecord, Boolean>> tmpSolid = t => t.Jiao_FZT == 2;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }


    }

}